package com.youlai.quarkus.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.quarkus.system.common.anno.annotation.PreventDuplicateSubmit;
import com.youlai.quarkus.system.common.model.Option;
import com.youlai.quarkus.system.common.result.PageResult;
import com.youlai.quarkus.system.common.result.Result;
import com.youlai.quarkus.system.model.form.DictForm;
import com.youlai.quarkus.system.model.form.DictTypeForm;
import com.youlai.quarkus.system.model.query.DictPageQuery;
import com.youlai.quarkus.system.model.query.DictTypePageQuery;
import com.youlai.quarkus.system.model.vo.DictPageVO;
import com.youlai.quarkus.system.model.vo.DictTypePageVO;
import com.youlai.quarkus.system.service.SysDictService;
import com.youlai.quarkus.system.service.SysDictTypeService;
import jakarta.ws.rs.*;
import lombok.RequiredArgsConstructor;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;
import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;

import java.util.List;

@Tag(name = "06.字典接口")
@Path("/api/v1/dict")
@RequiredArgsConstructor
public class SysDictController {

    private final SysDictService dictService;
    private final SysDictTypeService dictTypeService;

    @Operation(summary = "字典分页列表")
    @Path("/page")
    @GET
    public PageResult<DictPageVO> getDictPage(
            @QueryParam(value = "pageNum") Integer pageNum,
            @QueryParam(value = "pageSize") Integer pageSize,
            @QueryParam(value = "keywords") String keyWord,
            @QueryParam(value = "typeCode") String typeCode
    ) {
        DictPageQuery queryParams = new DictPageQuery(keyWord, typeCode);
        queryParams.setPageSize(pageSize);
        queryParams.setPageNum(pageNum);
        Page<DictPageVO> result = dictService.getDictPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "字典数据表单数据")
    @Path("/{id}/form")
    @GET
    public Result<DictForm> getDictForm(
            @Parameter(description ="字典ID") @PathParam("id") Long id
    ) {
        DictForm formData = dictService.getDictForm(id);
        return Result.success(formData);
    }

    @Operation(summary = "新增字典")
    @POST
    @PreventDuplicateSubmit
    public Result saveDict(
            @RequestBody DictForm DictForm
    ) {
        boolean result = dictService.saveDict(DictForm);
        return Result.judge(result);
    }

    @Operation(summary = "修改字典")
    @Path("/{id}")
    @PUT
    public Result updateDict(
            @PathParam("id") Long id,
            @RequestBody DictForm DictForm
    ) {
        boolean status = dictService.updateDict(id, DictForm);
        return Result.judge(status);
    }

    @Operation(summary = "删除字典")
    @Path("/{ids}")
    @DELETE
    public Result deleteDict(
            @Parameter(description ="字典ID，多个以英文逗号(,)拼接") @PathParam("ids") String ids
    ) {
        boolean result = dictService.deleteDict(ids);
        return Result.judge(result);
    }


    @Operation(summary = "字典下拉列表")
    @Path("/{typeCode}/options")
    @GET
    public Result<List<Option>> listDictOptions(
            @Parameter(description ="字典类型编码") @PathParam("typeCode") String typeCode
    ) {
        List<Option> list = dictService.listDictOptions(typeCode);
        return Result.success(list);
    }


    /*----------------------------------------------------*/
    @Operation(summary = "字典类型分页列表")
    @Path("/types/page")
    @GET
    public PageResult<DictTypePageVO> getDictTypePage(
            @QueryParam(value = "pageNum") Integer pageNum,
            @QueryParam(value = "pageSize") Integer pageSize,
            @QueryParam(value = "keywords") String keyWord
    ) {
        DictTypePageQuery queryParams = new DictTypePageQuery(keyWord);
        queryParams.setPage(pageNum, pageSize);
        Page<DictTypePageVO> result = dictTypeService.getDictTypePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "字典类型表单数据")
    @Path("/types/{id}/form")
    @GET
    public Result<DictTypeForm> getDictTypeForm(
            @Parameter(description ="字典ID") @PathParam("id") Long id
    ) {
        DictTypeForm dictTypeForm = dictTypeService.getDictTypeForm(id);
        return Result.success(dictTypeForm);
    }

    @Operation(summary = "新增字典类型")
    @Path("/types")
    @POST
    @PreventDuplicateSubmit
    public Result saveDictType(@RequestBody DictTypeForm dictTypeForm) {
        boolean result = dictTypeService.saveDictType(dictTypeForm);
        return Result.judge(result);
    }

    @Operation(summary = "修改字典类型")
    @Path("/types/{id}")
    @PUT
    public Result updateDictType(@PathParam("id") Long id, @RequestBody DictTypeForm dictTypeForm) {
        boolean status = dictTypeService.updateDictType(id, dictTypeForm);
        return Result.judge(status);
    }

    @Operation(summary = "删除字典类型")
    @Path("/types/{ids}")
    @DELETE
    public Result deleteDictTypes(
            @Parameter(description ="字典类型ID，多个以英文逗号(,)分割") @PathParam("ids") String ids
    ) {
        boolean result = dictTypeService.deleteDictTypes(ids);
        return Result.judge(result);
    }

}
